from rest_framework.generics import ListAPIView
from django.core.cache import cache
from rest_framework.response import Response
from django.conf import settings

from . import models, serializers


# Create your views here.

class BannerListAPIView(ListAPIView):
    queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('-order')[:settings.BANNER_COUNT]
    serializer_class = serializers.BannerModelSerializer

    def get(self, request, *args, **kwargs):
        banner_list = cache.get('banner_list')
        if not banner_list:
            response = self.list(request, *args, **kwargs)
            cache.set('banner_list', response.data)
            return response
        return Response(banner_list)
